package com.house.web.controller.system;

import java.io.IOException;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.house.common.config.HouseConfig;
import com.house.common.core.controller.BaseController;
import com.house.common.core.domain.AjaxResult;
import com.house.common.core.domain.entity.House;
import com.house.common.core.domain.entity.User;
import com.house.common.core.domain.model.HouseInfo;
import com.house.common.core.domain.model.LoginUser;
import com.house.common.core.page.TableDataInfo;
import com.house.common.exception.ServiceException;
import com.house.common.utils.StringUtils;
import com.house.common.utils.file.FileUploadUtils;
import com.house.system.service.IHouseService;
import com.house.system.service.IMediumService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * 房屋信息Controller
 *
 * @author tc
 * @date 2022-02-04
 */
@RestController
@RequestMapping("/entity/house")
public class HouseController extends BaseController {
    @Autowired
    private IHouseService houseService;

    @Autowired
    private IMediumService mediumService;

    /**
     * 查询房屋信息列表
     */
    @PreAuthorize("@ss.hasPermi('entity:house:list')")
    @GetMapping("/list")
    public TableDataInfo list(House house) {
        startPage();
        List<House> list = houseService.selectHouseList(house);
        return getDataTable(list);
    }

    /**
     * 获取房屋信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('entity:house:query')")
    @GetMapping(value = "/{houseId}")
    public AjaxResult getInfo(@PathVariable("houseId") Long houseId) {
        return AjaxResult.success(houseService.selectHouseByHouseId(houseId));
    }

    /**
     * 新增房屋信息
     */
    @PreAuthorize("@ss.hasPermi('entity:house:add')")
    @PostMapping
    public AjaxResult add(@RequestBody HouseInfo houseInfo) {
        AjaxResult ajax = AjaxResult.success();
        //中介发布房源.判断个人证件+邮箱是否不为null
        User user = getLoginUser().getUser();
        if (StringUtils.isNull(user.getEmail()) && StringUtils.isEmpty(user.getEmail())) {
            throw new ServiceException("请填写邮箱!!!", 400);
        } else if (StringUtils.isNull(user.getDocuments()) && StringUtils.isEmpty(user.getDocuments())) {
            throw new ServiceException("请上传个人证件!!!", 400);
        } else {
            houseInfo.setUserId(getUserId());
            Long houseId = houseService.insertHouse(houseInfo);
            ajax.put("houseId", houseId);
        }
        return ajax;
    }

    /**
     * 修改房屋信息
     */
    @PreAuthorize("@ss.hasPermi('entity:house:edit')")
    @PutMapping
    public AjaxResult edit(@RequestBody House house) {
        return toAjax(houseService.updateHouse(house));
    }

    /**
     * 删除房屋信息
     */
    @PreAuthorize("@ss.hasPermi('entity:house:remove')")
    @DeleteMapping("/{houseId}")
    public AjaxResult remove(@PathVariable Long houseId) {
        return toAjax(houseService.deleteHouseByHouseId(houseId));
    }

    /**
     * 中介房产证上传
     */
    @PreAuthorize("@ss.hasPermi('entity:house:add')")
    @PostMapping("/upload/estate")
    public AjaxResult documents(@RequestParam("estatefile") MultipartFile file, @RequestParam("houseId") Long houseId) throws IOException {
        if (!file.isEmpty()) {
            String estate = FileUploadUtils.upload(HouseConfig.getEstatePath(), file);
            if (mediumService.updateMediumEstate(houseId, estate)) {
                AjaxResult ajax = AjaxResult.success();
                System.out.println("imgUrl: " + estate);
                ajax.put("imgUrl", estate);
                return ajax;
            }
        }
        return AjaxResult.error("上传图片异常，请联系管理员");
    }

    /**
     * 中介房源图片上传
     */
    @PreAuthorize("@ss.hasPermi('entity:house:add')")
    @PostMapping("/uploadHouseImg")
    public AjaxResult houseImg(@RequestParam("houseImg") MultipartFile file, @RequestParam("houseId") Long houseId) throws IOException {
        if (!file.isEmpty()) {
            LoginUser loginUser = getLoginUser();
            String houseImg = FileUploadUtils.upload(HouseConfig.getHouseImgPath(), file);
            if (houseService.updateHouseImg(houseId, houseImg)) {
                AjaxResult ajax = AjaxResult.success();
                System.out.println("imgUrl: " + houseImg);
                ajax.put("imgUrl", houseImg);
                return ajax;
            }
        }
        return AjaxResult.error("上传图片异常，请联系管理员");
    }

    /**
     * 管理员正式发布房源
     */
    @PreAuthorize("@ss.hasPermi('entity:house:edit')")
    @PostMapping("/release")
    public AjaxResult release(@RequestBody House house) {
        house.setStatus("0");
        house.setIsHandle("1");
        return toAjax(houseService.updateHouse(house));
    }
}
